#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fun>
static const COLOR[] = "^x04" //green
static const CONTACT[] = ""
new maxplayers
new gmsgSayText
new mpd, mkb, mhb
new g_MsgSync
new health_add
new health_hs_add
new health_max
new nKiller
new nKiller_hp
new nHp_add
new nHp_max
new g_vip_active
new g_menu_active
new bool:HasC4[33]
new gPlayerChoice[33]
new gPlayerOldMenu[33]

#define DAMAGE_RECIEVED
#define Keysrod (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<9) // Keys: 1234567890
#define KeysrodOld (1<<0)|(1<<1)|(1<<9) // Keys: 1234567890

new round;

/* #if defined DAMAGE_RECIEVED
	new g_MsgSync2
#endif */

public plugin_init()
{
	register_plugin("VIP PL Version", "2.02", "BotQ")
	mpd = register_cvar("money_per_damage","3")
	mkb = register_cvar("money_kill_bonus","500")
	mhb = register_cvar("money_hs_bonus","300")
	health_add = register_cvar("amx_vip_hp", "15")
	health_hs_add = register_cvar("amx_vip_hp_hs", "30")
	health_max = register_cvar("amx_vip_max_hp", "100")
	g_vip_active = register_cvar("vip_active", "0")
	g_menu_active = register_cvar("menu_active", "1")

	register_event("Damage","Damage","b")
	register_event("DeathMsg","death_msg","a")
	register_logevent("Round_Start", 2, "1=Round_Start")
	register_logevent("Round_Reset", 2, "1=Game_Commencing")
	register_event("TextMsg", "Round_Reset", "a", "2&Game_will_restart_in")
	register_event("DeathMsg", "hook_death", "a", "1>0")

	maxplayers = get_maxplayers()

	register_event("Damage", "on_damage", "b", "2!0", "3=0", "4!0")

	g_MsgSync = CreateHudSyncObj()

/* #if defined DAMAGE_RECIEVED
	g_MsgSync2 = CreateHudSyncObj()
#endif */
	register_menucmd(register_menuid("rod"), Keysrod, "Pressedrod")
	register_menucmd(register_menuid("rodOld"), KeysrodOld, "Pressedrod")
//	register_clcmd("awp","HandleCmd")
	register_clcmd("sg550","HandleCmd")
	register_clcmd("g3sg1","HandleCmd")
//	register_event("CurWeapon", "event_CurWeapon_AWP", "be", "1=1")
	register_event("CurWeapon", "event_CurWeapon_SG550", "be", "1=1")
	register_event("CurWeapon", "event_CurWeapon_G3SG1", "be", "1=1")
	register_event("CurWeapon", "event_CurWeapon_M249", "be", "1=1")
	register_clcmd("say /vip","ShowMotd")
	register_clcmd("say /oldmenu","setOldMenu")
	register_clcmd("say /newmenu","setNewMenu")
	register_clcmd("say /vipmenu","EnableVipMenu")
	register_clcmd("say vipmenu","EnableVipMenu")
	gmsgSayText = get_user_msgid("SayText")
	register_clcmd("say", "handle_say")
	register_cvar("sv_contact", CONTACT, FCVAR_SERVER)
}
public setOldMenu(id){
	gPlayerOldMenu[id] = 1;
}
public setNewMenu(id){
	gPlayerOldMenu[id] = 0;
}

public on_damage(id)
{
	new attacker = get_user_attacker(id)

#if defined DAMAGE_RECIEVED
	// id should be connected if this message is sent, but lets check anyway
	if ( is_user_connected(id) && is_user_connected(attacker) )
	if (get_user_flags(attacker) & ADMIN_LEVEL_H)
	{
		new damage = read_data(2)

//		set_hudmessage(255, 0, 0, 0.45, 0.50, 2, 0.1, 4.0, 0.1, 0.1, -1)
//		ShowSyncHudMsg(id, g_MsgSync2, "%i^n", damage)
#else
	if ( is_user_connected(attacker) && if (get_user_flags(attacker) & ADMIN_LEVEL_H) )
	{
		new damage = read_data(2)
#endif
		set_hudmessage(0, 100, 200, -1.0, 0.55, 2, 0.1, 4.0, 0.02, 0.02, -1)
		ShowSyncHudMsg(attacker, g_MsgSync, "%i^n", damage)
	}
}

public Damage(id)
{
	new weapon, hitpoint, attacker = get_user_attacker(id,weapon,hitpoint)
	if(attacker<=maxplayers && is_user_alive(attacker) && attacker!=id)
	if (get_user_flags(attacker) & ADMIN_LEVEL_H) 
	{
		new money = read_data(2) * get_pcvar_num(mpd)
		if(hitpoint==1) money += get_pcvar_num(mhb)
		cs_set_user_money(attacker,cs_get_user_money(attacker) + money)
	}
}

public death_msg()
{
	if(read_data(1)<=maxplayers && read_data(1) && read_data(1)!=read_data(2)) cs_set_user_money(read_data(1),cs_get_user_money(read_data(1)) + get_pcvar_num(mkb) - 300)
}

public Round_Start()
{
	round++;
	new players[32], player, pnum;
	get_players(players, pnum, "a");
	for(new i = 0; i < pnum; i++)
	{
		player = players[i];
		if(get_user_flags(player) & ADMIN_LEVEL_H)
		{
			if(!get_pcvar_num(g_menu_active))
				return PLUGIN_CONTINUE
			if(!is_user_hltv(player) && !is_user_bot(player))
			{
				give_item(player, "weapon_hegrenade");
				give_item(player, "weapon_flashbang");
				give_item(player, "weapon_flashbang");
				give_item(player, "weapon_smokegrenade");
				give_item(player, "item_assaultsuit");
				give_item(player, "item_thighpack");
			}
			if(round > 3) Showrod(player)
		}
	}
	return PLUGIN_HANDLED
}
public client_connect(id){
	gPlayerChoice[id] = 0;
	gPlayerOldMenu[id] = 0;
	return PLUGIN_CONTINUE;
}
public Round_Reset()
{
	round = 0;
	for(new i=0;i<33;i++)
		gPlayerChoice[i] = 0;
}

public hook_death()
{
   // Killer id
   nKiller = read_data(1)
   if(!(get_user_flags(nKiller) & ADMIN_LEVEL_H))
   return;
   
   if ( (read_data(3) == 1) && (read_data(5) == 0) )
   {
      nHp_add = get_pcvar_num (health_hs_add)
   }
   else
      nHp_add = get_pcvar_num (health_add)
   nHp_max = get_pcvar_num (health_max)
   // Updating Killer HP
   

   nKiller_hp = get_user_health(nKiller)
   if(nKiller_hp >=100)
   return;
   nKiller_hp += nHp_add
   // Maximum HP check
   if (nKiller_hp > nHp_max) nKiller_hp = nHp_max
   set_user_health(nKiller, nKiller_hp)
   // Hud message "Healed +15/+30 hp"
   set_hudmessage(0, 255, 0, -1.0, 0.15, 0, 1.0, 1.0, 0.1, 0.1, -1)
   show_hudmessage(nKiller, "Healed +%d hp", nHp_add)
   // Screen fading
   message_begin(MSG_ONE, get_user_msgid("ScreenFade"), {0,0,0}, nKiller)
   write_short(1<<10)
   write_short(1<<10)
   write_short(0x0000)
   write_byte(0)
   write_byte(0)
   write_byte(200)
   write_byte(75)
   message_end()
 
}

public Showrod(id) {
	new key = gPlayerChoice[id];
	new oldMenu = gPlayerOldMenu[id];
	if(oldMenu)
		client_print(id, print_chat, "Wpisz /newmenu aby uzyskac nowe menu VIP'a");
	else
		client_print(id, print_chat, "Wpisz /oldmenu aby uzyskac stare menu VIP'a");
		
	if(!oldMenu && (key == 2 || key == 3 || key == 4)) {
		Pressedrod(id, key)
		client_print(id, print_chat, "Wpisz /vipmenu aby uzyskac menu w nastepnej rundzie");
	} else {
		new msg[256];
		copy(msg, charsmax(msg),"\rVIP Menu");
		add(msg, charsmax(msg), "^n\d1. \wWez \yM4A1+Deagle ");
		add(msg, charsmax(msg), "^n\d2. \wWez \yAK47+Deagle ");
		if(!oldMenu){
			add(msg, charsmax(msg), "^n\d3. \wWez \yM4A1+Deagle i nie pytaj wiecej");
			add(msg, charsmax(msg), "^n\d4. \wWez \yAK47+Deagle i nie pytaj wiecej");
			add(msg, charsmax(msg), "^n\d5. \wNic i nie pytaj wiecej");
		}
		add(msg, charsmax(msg), "^n^n\d0. Wyjscie");
		if(oldMenu)
			show_menu(id, KeysrodOld, msg, -1, "rodOld") // Display menu
		else
			show_menu(id, Keysrod, msg, -1, "rod") // Display menu
	}
}
public Pressedrod(id, key) {
	/* Menu:
	* VIP Menu
	* 1. Wez M4A1 + Deagle
	* 2. Wez AK47+Deagle
	* 3. Wez M4A1 + Deagle i nie pytaj wiecej
	* 4. Wez AK47 + Deagle i nie pytaj wiecej
	* 5. Nic i nie pytaj wiecej
	* 0. Exit
	*/
	gPlayerChoice[id] = key;
	switch (key) {
		case 0,2: { 
			if (user_has_weapon(id, CSW_C4) && get_user_team(id) == 1)
				HasC4[id] = true;
			else
				HasC4[id] = false;
            
			strip_user_weapons (id)
			give_item(id,"weapon_m4a1")
			give_item(id,"ammo_556nato")
			give_item(id,"ammo_556nato")
			give_item(id,"ammo_556nato")
			give_item(id,"weapon_deagle")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"weapon_knife")
			if(get_user_flags(id) & ADMIN_RCON)
			{
				give_item(id, "weapon_hegrenade");
				give_item(id, "weapon_flashbang");
				give_item(id, "weapon_flashbang");
				give_item(id, "weapon_smokegrenade");
				client_print(id, print_center, "Otrzymales M4A1, Deagle, HE, Smoke i 2x Flesh!")
			}
			else
			{
				give_item(id, "weapon_hegrenade");
			}
			give_item(id, "item_assaultsuit");
			give_item(id, "item_thighpack");
			client_print(id, print_center, "Wziales M4A1, Deagle i HE!")
			
			if (HasC4[id])
			{
				give_item(id, "weapon_c4");
				cs_set_user_plant( id );
			}
		}
		case 1,3: { 
			if (user_has_weapon(id, CSW_C4) && get_user_team(id) == 1)
				HasC4[id] = true;
			else
				HasC4[id] = false;
            
			strip_user_weapons (id)
			give_item(id,"weapon_ak47")
			give_item(id,"ammo_762nato")
			give_item(id,"ammo_762nato")
			give_item(id,"ammo_762nato")
			give_item(id,"weapon_deagle")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"ammo_50ae")
			give_item(id,"weapon_knife")
			if(get_user_flags(id) & ADMIN_RCON)
			{
				give_item(id, "weapon_hegrenade");
				give_item(id, "weapon_flashbang");
				give_item(id, "weapon_flashbang");
				give_item(id, "weapon_smokegrenade");
				client_print(id, print_center, "Wziales AK47, Deagle, HE")
			}
			else
			{
				give_item(id, "weapon_hegrenade");
				client_print(id, print_center, "Otrzymales AK47, Deagle i HE!")
			}
			give_item(id, "item_assaultsuit");
			give_item(id, "item_thighpack");
			
			if (HasC4[id])
				{
					give_item(id, "weapon_c4");
					cs_set_user_plant( id );
				}
			}
		case 4,9: {
			// 0
			client_print(id, print_center, "Otrzymales Granaty!")
		}
	}
	return PLUGIN_CONTINUE
}

public HandleCmd(id){
	if (!get_pcvar_num(g_vip_active))
      return PLUGIN_CONTINUE
	if(get_user_flags(id) & ADMIN_LEVEL_H) 
		return PLUGIN_CONTINUE
	client_print(id, print_center, "Niektore bronie sa tylko dla VIPow!")
	return PLUGIN_HANDLED
}

/*
public event_CurWeapon_AWP(id){
	if (!get_pcvar_num(g_vip_active))
      return PLUGIN_CONTINUE
	if(read_data(2) == CSW_AWP){
    	if(!(get_user_flags(id) & ADMIN_LEVEL_H)){
            client_print(id, print_center, "Snajperka 'AWP' tylko dla VIPow!")
            client_cmd(id, "drop")
        }
    }
	return PLUGIN_HANDLED
}
*/

public event_CurWeapon_SG550(id){
	if (!get_pcvar_num(g_vip_active))
      return PLUGIN_CONTINUE
	if(read_data(2) == CSW_SG550){
    	if(!(get_user_flags(id) & ADMIN_LEVEL_H)){
            client_print(id, print_center, "AutoKampa 'SG550' tylko dla VIPow!")
            client_cmd(id, "drop")
        }
    }
	return PLUGIN_HANDLED
}

public event_CurWeapon_G3SG1(id){
	if (!get_pcvar_num(g_vip_active))
      return PLUGIN_CONTINUE

	if(read_data(2) == CSW_G3SG1){
    	if(!(get_user_flags(id) & ADMIN_LEVEL_H)){
            client_print(id, print_center, "AutoKampa 'G3SG1' tylko dla VIPow!")
            client_cmd(id, "drop")
        }
    }
	return PLUGIN_HANDLED
}

public event_CurWeapon_M249(id){
	if (!get_pcvar_num(g_vip_active))
      return PLUGIN_CONTINUE

	if(read_data(2) == CSW_M249){
    	if(!(get_user_flags(id) & ADMIN_LEVEL_H)){
            client_print(id, print_center, "Bron 'M249 Para' tylko dla VIPow")
            client_cmd(id, "drop")
        }
    }
	return PLUGIN_HANDLED
}

public ShowMotd(id)
{
	show_motd(id, "vip.txt")
}

public EnableVipMenu(id){
	if(get_user_flags(id) & ADMIN_LEVEL_H)
	{
		if(!get_pcvar_num(g_menu_active))
			return PLUGIN_CONTINUE
		gPlayerChoice[id] = 0;
		client_print(id, print_center, "Menu VIP'a pojawi sie w nastepnej rundzie.");
	}
	return PLUGIN_CONTINUE;
}

public handle_say(id) {
	new said[192]
	read_args(said,192)
	if( ( containi(said, "who") != -1 && containi(said, "admin") != -1 ) || contain(said, "/vips") != -1 )
		set_task(0.1,"print_adminlist",id)
	return PLUGIN_CONTINUE
}

public print_adminlist(user) 
{
	new adminnames[33][32]
	new message[256]
	new contactinfo[256], contact[112]
	new id, count, x, len
	
	for(id = 1 ; id <= maxplayers ; id++)
	{
		if(is_user_connected(id))
		{
			if(get_user_flags(id) & ADMIN_LEVEL_H && !(get_user_flags(id) & ADMIN_IMMUNITY)) 
				get_user_name(id, adminnames[count++], 31)	
		}
	}
	
	len = format(message, 255, "%s VIP'y ONLINE: ",COLOR)
	if(count > 0) 
	{
		for(x = 0 ; x < count ; x++) 
		{
			len += format(message[len], 255-len, "%s%s%s ", COLOR, adminnames[x], x < (count-1) ? "^x01, ":"")
			if(len > 96 ) {
				print_message(user, message)
				len = format(message, 255, "%s ",COLOR)
			}
		}
		print_message(user, message)
	}
	else {
		len += format(message[len], 255-len, "Brak Vip'ow Online")
		print_message(user, message)
	}
	
	get_cvar_string("sv_contact", contact, 63)
	if(contact[0])  {
		format(contactinfo, 111, "%s Kontakt z Adminem -- %s", COLOR, contact)
		print_message(user, contactinfo)
	}
	return PLUGIN_HANDLED;
}

print_message(id, msg[]) {
	message_begin(MSG_ONE, gmsgSayText, {0,0,0}, id)
	write_byte(id)
	write_string(msg)
	message_end()
}

